/**
 * 苹果账号管理管理初始化
 */
var AppleIds = {
    id: "AppleIdsTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    usedTable: null,
    closedTable: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
AppleIds.initColumn = function () {
    return [[
        {title: '账号', field: 'account', align: 'center',width: 280},
        {title: '已用设备数量', field: 'usedDeviceCount', align: 'center'},
        {title: '剩余设备数量', field: 'leftDeviceCount',  align: 'center'},
        {
            title: '有效期', field: 'validDate',
            align: 'center', templet: function (res) {
                if (!$.trim(res.validDate)) {
                    return "";
                } else {
                    return new Date(res.validDate).format("yyyy-MM-dd");
                }
            }
        },
        {
            title: '导入时间', field: 'importDate',
            align: 'center', templet: function (res) {
                if (!$.trim(res.importDate)) {
                    return "";
                } else {
                    return new Date(res.importDate).format("yyyy-MM-dd");
                }
            }
        },
        {
            title: '状态', field: 'status',
            align: 'center', templet: function (res) {
                return '正常'
            }
        }

    ]];
};

/**
 * 已使用完列表
 * @returns {*[][]}
 */
AppleIds.initUsedColumn = function () {
    return [[
        {title: '账号', field: 'account', align: 'center',width: 280},
        {title: '已用设备数量', field: 'usedDeviceCount', align: 'center'},
        {title: '剩余设备数量', field: 'leftDeviceCount',  align: 'center'},
        {
            title: '有效期', field: 'validDate',
            align: 'center', templet: function (res) {
                if (!$.trim(res.validDate)) {
                    return "";
                } else {
                    return new Date(res.validDate).format("yyyy-MM-dd");
                }
            }
        },
        {
            title: '导入时间', field: 'importDate',
            align: 'center', templet: function (res) {
                if (!$.trim(res.importDate)) {
                    return "";
                } else {
                    return new Date(res.importDate).format("yyyy-MM-dd");
                }
            }
        },
        {
            title: '状态', field: 'status',
            align: 'center', templet: function (res) {
                return '正常'
            }
        }
    ]];
};

/**
 * 被封列表样式
 * @returns {*[][]}
 */
AppleIds.initClosedColumn = function () {
    return [[
        {title: '账号', field: 'account', align: 'center',width: 280},
        {title: '已用设备数量', field: 'usedDeviceCount', align: 'center'},
        {title: '剩余设备数量', field: 'leftDeviceCount',  align: 'center'},
        {
            title: '有效期', field: 'validDate',
            align: 'center', templet: function (res) {
                if (!$.trim(res.validDate)) {
                    return "";
                } else {
                    return new Date(res.validDate).format("yyyy-MM-dd");
                }
            }
        },
        {
            title: '导入时间', field: 'importDate',
            align: 'center', templet: function (res) {
                if (!$.trim(res.importDate)) {
                    return "";
                } else {
                    return new Date(res.importDate).format("yyyy-MM-dd");
                }
            }
        },
        {
            title: '被封时间', field: 'blockedDate',
            align: 'center', templet: function (res) {
                if (!$.trim(res.blockedDate)) {
                    return "";
                } else {
                    return new Date(res.blockedDate).format("yyyy-MM-dd");
                }
            }
        },
        {
            title: '状态', field: 'status',
            align: 'center', templet: function (res) {
                return '已封'
            }
        }

    ]];
};

AppleIds.changeStatus = function(appId,requestStatus){
    layer.confirm("注意：状态一旦修改，不可撤销？",
        {btn:['确认','取消']},
        function () {
            layer.closeAll()
            layer.load(0,{
                shade:false
            });

            var ajax = new $ax(Feng.ctxPath + "/appleIds/changeStatus", function (data) {
                Feng.success("修改成功!");
                AppleIds.refresh();
                layer.closeAll();
            }, function (data) {
                layer.closeAll();
                Feng.error("修改失败!" + data.responseJSON.message + "!");
            });
            ajax.set("appId", appId);
            ajax.set("status", requestStatus);
            ajax.start();
        },function () {
            layer.msg("操作已取消",{icon:1})
        });
}

AppleIds.refresh = function () {
    AppleIds.refreshTable1();
    AppleIds.refreshTable2();
    AppleIds.refreshTable3();
}
/**
 * 刷新未使用账号列表
 */
AppleIds.refreshTable1 = function(){
    AppleIds.table.reload("tableReload", {
        where: {"appleType":0},
        page: {
            curr: 1 //重新从第1页开始
        }
    });
}
/**
 * 刷新已使用账号列表
 */
AppleIds.refreshTable2 = function(){
    AppleIds.usedTable.reload("usedTableReload", {
        where: {"appleType":1},
        page: {
            curr: 1 //重新从第1页开始
        }
    });
}
/**
 * 刷新已封账号列表
 */
AppleIds.refreshTable3 = function(){
    AppleIds.closedTable.reload("closedTableReload", {
        where: {"appleType":2},
        page: {
            curr: 1 //重新从第1页开始
        }
    });
}

/**
 * 账户列表
 * @param type 列表类别：0-未使用，1 - 已使用，2-已被封
 * @param where 条件
 * @param tableId 列表的唯一ID
 */
AppleIds.tableList = function (type) {
    layui.use("table", function () {
        var table = layui.table;
        var tableId = "tableReload";
        var initCol = AppleIds.initUsedColumn();
        var elem = "#table";
        var where = {"appleType":0};
        if (type === 0){
            AppleIds.table = table;
            initCol = AppleIds.initColumn();
            tableId = "tableReload";
            where = {"appleType":0};
            elem = "#table";
        }else if (type === 1){
            AppleIds.usedTable = table;
            initCol = AppleIds.initUsedColumn();
            where = {"appleType":1};
            tableId = "usedTableReload";
            elem = "#usedTable";
        }else if(type === 2){
            AppleIds.closedTable = table;
            initCol = AppleIds.initClosedColumn();
            where = {"appleType":2};
            tableId = "closedTableReload";
            elem = "#closedTable";
        }

        table.render({
            elem: elem,
            url: Feng.ctxPath + "/appleIds/list",
            cellMinWidth: 120,
            where: where,
            page: true,
            limit: 20,
            cols: initCol,
            parseData: function (res) {
                return {
                    "code": 0,
                    "msg": "成功",
                    "count": res.total,
                    "data": res.items
                }
            },
            text: {
                none: '暂无相关数据'
            },
            id: tableId
        });
    });
};

/**
 * 打开文件导入窗口
 */
AppleIds.openImportAppleIds = function () {
    var index = layer.open({
        type: 2,
        title: '导入苹果账号',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/appleIds/appleIds_import_excel'
    });
    this.layerIndex = index;
};
/**
 * tab切换的js方法监听
 */
AppleIds.initTabChange = function(){
    layui.use('element', function(){
        var element = layui.element; //Tab的切换功能，切换事件监听等，需要依赖element模块

        element.on('tab(tabList)', function(elem){
            if (0 == elem.index){
                if (AppleIds.table == null){
                    AppleIds.tableList(0);
                }else{
                    AppleIds.refreshTable1();
                }
            }else if(1== elem.index){
                if (AppleIds.usedTable == null){
                    AppleIds.tableList(1);
                }else{
                    AppleIds.refreshTable2();
                }
            }else{
                if (AppleIds.closedTable == null){
                    AppleIds.tableList(2);
                }else{
                    AppleIds.refreshTable3();
                }
            }
        });
    });
}

AppleIds.exportExcel = function(){
    var type = $("#type").val();
    var month = $("#month").val();
    console.log(type);
    console.log(month);

    if (type == 1){
        var hrefUrl =
            Feng.ctxPath + "/appleIds/downloadExcel" +
            "?type="+type+"&month="+month;
        window.location.href = hrefUrl;
    }else {
        if (month == null || month == ''){
            Feng.error("请选择月份");
        }else{
            var hrefUrl =
                Feng.ctxPath + "/appleIds/downloadExcel" +
                "?type="+type+"&month="+month;
            window.location.href = hrefUrl;
        }
    }
};
/**
 * 选择切换类型选项
 */
AppleIds.selectType = function(){
    var type = $("#type").val();
    if (type == 1){
        $("#month").hide();
    }else{
        $("#month").show();
    }
};

$(function () {
    $("#month").hide();
    AppleIds.initTabChange();
    DateUtils.expandDate();
    AppleIds.tableList(0);
    AppleIds.tableList(1);
    AppleIds.tableList(2);
});
